package RPPM.servlet;

import RPPM.domain.Executor;
import RPPM.domain.Payment;
import RPPM.domain.Teacher;
import RPPM.domain.ResultInfo;
import RPPM.service.Impl.StuServiceImpl;
import RPPM.service.ManagerService;
import RPPM.service.Impl.ManagerServiceImpl;
import RPPM.service.StuService;
import RPPM.servlet.BaseServlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
1.接收申请表
2.提供讲师信息
3.提供培训计划
4.寻找执行人

 */
@WebServlet("/managerservlet/*")
public class ManagerServlet extends BaseServlet{

    /**
     * ossCheckStudents 获取全部课程的全部学生，mg
     * @param request
     * @param response
     * @throws IOException
     */
    public void mngCheckStudents(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("mngCheckStudents");

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> students = new ArrayList<>();

        students = managerService.getStudents();
        System.out.println(students);

        ResultInfo info = new ResultInfo();
        info.setData(students);

        writeJsonValue(response, info);
    }

    public void mngCheckTeachers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("mngCheckTeachers");

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> teachers = new ArrayList<>();

        teachers = managerService.getTeachers();
        System.out.println(teachers);

        ResultInfo info = new ResultInfo();
        info.setData(teachers);

        writeJsonValue(response, info);
    }

    public void mngCheckPlans(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("mngCheckPlans");

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> plans = new ArrayList<>();

        plans = managerService.getPlans();
        System.out.println(plans);

        for (Map<String, Object> plan : plans) {
            int couPass = (int) plan.get("Cou_pass");
            if (couPass == 1) {
                plan.put("Cou_pass", "已通过");
            } else if (couPass == 0) {
                plan.put("Cou_pass", "未通过");
            }
        }

        ResultInfo info = new ResultInfo();
        info.setData(plans);

        writeJsonValue(response, info);
    }

    public void mngChangeStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngChangeStudents");

        //int stu_id = (int) request.getSession().getAttribute("stuid");
        String searchText = request.getParameter("searchText");
        String i = request.getParameter("i");
        String column = request.getParameter("column");
        String change = request.getParameter("change");
        System.out.println(i);
        System.out.println(column);
        System.out.println(change);

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> students = new ArrayList<>();

        students = managerService.findStudent(searchText);

        Map<String, Object> changestu = students.get(Integer.parseInt(i)-1);
        System.out.println(changestu);

        int stuid = (int) changestu.get("Stu_id");
        System.out.println(stuid);

        boolean del = false;

        del = managerService.changeStudent(stuid, column, change);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void mngChangeTeachers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngChangeTeachers");

        //int stu_id = (int) request.getSession().getAttribute("stuid");
        String searchText = request.getParameter("searchText");
        String i = request.getParameter("i");
        String column = request.getParameter("column");
        String change = request.getParameter("change");

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> teachers = new ArrayList<>();

        teachers = managerService.findTeacher(searchText);

        Map<String, Object> changetea = teachers.get(Integer.parseInt(i)-1);
        System.out.println(changetea);

        int teaid = (int) changetea.get("Tea_id");
        System.out.println(teaid);

        boolean del = false;

        del = managerService.changeTeacher(teaid, column, change);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void mngChangePlans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngChangePlans");

        //int stu_id = (int) request.getSession().getAttribute("stuid");
        String searchText = request.getParameter("searchText");
        String i = request.getParameter("i");
        String column = request.getParameter("column");
        String change = request.getParameter("change");

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> plans = new ArrayList<>();

        plans = managerService.findPlan(searchText);

        Map<String, Object> changeplan = plans.get(Integer.parseInt(i)-1);
        System.out.println(changeplan);

        int couid = (int) changeplan.get("Cou_id");
        System.out.println(couid);

        boolean del = false;

        del = managerService.changePlan(couid, column, change);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void mngDeleteStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngDeleteStudents");

        String searchText = request.getParameter("searchText");
        System.out.println("searchText: "+ searchText);
        //int stu_id = (int) request.getSession().getAttribute("stuid");
        String tmp = request.getParameter("i");
        System.out.println(tmp);

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> students = new ArrayList<>();

        students = managerService.findStudent(searchText);            // 查了全部学生，所以搜索后选择序号会出问题

        Map<String, Object> delstu = students.get(Integer.parseInt(tmp)-1);
        System.out.println(delstu);

        int stuid = (int) delstu.get("Stu_id");
        System.out.println(stuid);

        boolean del = false;

        del = managerService.deleteStudent(stuid);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void mngDeleteTeachers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngDeleteTeachers");

        String searchText = request.getParameter("searchText");
        String tmp = request.getParameter("i");
        System.out.println(tmp);

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> teachers = new ArrayList<>();

        teachers = managerService.findTeacher(searchText);

        Map<String, Object> deltea = teachers.get(Integer.parseInt(tmp)-1);
        System.out.println(deltea);

        int teaid = (int) deltea.get("Tea_id");
        System.out.println(teaid);

        boolean del = false;

        del = managerService.deleteTeacher(teaid);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void mngDeletePlans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngDeletePlans");

        String searchText = request.getParameter("searchText");
        String tmp = request.getParameter("i");
        System.out.println(tmp);

        ManagerService managerService = new ManagerServiceImpl();

        List<Map<String, Object>> plans = new ArrayList<>();

        plans = managerService.findPlan(searchText);

        Map<String, Object> delplan = plans.get(Integer.parseInt(tmp)-1);
        System.out.println(delplan);

        int couid = (int) delplan.get("Cou_id");
        System.out.println(couid);

        boolean del = false;

        del = managerService.deletePlan(couid);
        System.out.println(del);

        ResultInfo info = new ResultInfo();
        info.setFlag(del);

        writeJsonValue(response, info);
    }

    public void addNewPlan(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("addNewPlan");

        String input0 = request.getParameter("input0");
        String input1 = request.getParameter("input1");
        String input2 = request.getParameter("input2");
        String input3 = request.getParameter("input3");
        String input4 = request.getParameter("input4");
        String input5 = request.getParameter("input5");
        String input6 = request.getParameter("input6");

        //System.out.println(input0);
        //System.out.println(input1);
        //System.out.println(input2);
        //System.out.println(input3);
        //System.out.println(input4);
        //System.out.println(input5);
        //System.out.println(input6);

        // 创建一个List<Map<String, Object>>变量用于存储数据
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 创建一个Map对象，存储前端发送的数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("Cou_name", input0);
        dataMap.put("Cou_content", input1);
        dataMap.put("Cou_location", input2);
        dataMap.put("Cou_teacher", input3);
        dataMap.put("Cou_payment", input4);
        dataMap.put("Com_name", input5);
        dataMap.put("Com_phone", input6);

        // 将Map对象添加到List中
        dataList.add(dataMap);

        System.out.println(dataList);

        ManagerService managerService = new ManagerServiceImpl();

        boolean plan = false;

        plan = managerService.addPlan(dataList);
        System.out.println(plan);

        ResultInfo info = new ResultInfo();
        info.setFlag(plan);

        writeJsonValue(response, info);
    }

    public void addNewStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("addNewStudent");

        String input0 = request.getParameter("input0");
        String input1 = request.getParameter("input1");
        String input2 = request.getParameter("input2");
        String input3 = request.getParameter("input3");
        String input4 = request.getParameter("input4");
        String input5 = request.getParameter("input5");

        //System.out.println(input0);
        //System.out.println(input1);
        //System.out.println(input2);
        //System.out.println(input3);
        //System.out.println(input4);
        //System.out.println(input5);
        //System.out.println(input6);

        // 创建一个List<Map<String, Object>>变量用于存储数据
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 创建一个Map对象，存储前端发送的数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("Stu_name", input0);
        dataMap.put("Stu_sex", input1);
        dataMap.put("Stu_level", input2);
        dataMap.put("Stu_company", input3);
        dataMap.put("Stu_post", input4);
        dataMap.put("Stu_phone", input5);

        // 将Map对象添加到List中
        dataList.add(dataMap);

        System.out.println(dataList);

        ManagerService managerService = new ManagerServiceImpl();

        boolean stu = false;

        stu = managerService.addStudent(dataList);
        System.out.println(stu);

        ResultInfo info = new ResultInfo();
        info.setFlag(stu);

        writeJsonValue(response, info);
    }

    public void addNewTeacher(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("addNewPlan");

        String input0 = request.getParameter("input0");
        String input1 = request.getParameter("input1");
        String input2 = request.getParameter("input2");
        String input3 = request.getParameter("input3");
        String input4 = request.getParameter("input4");

        // 创建一个List<Map<String, Object>>变量用于存储数据
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 创建一个Map对象，存储前端发送的数据
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("Tea_name", input0);
        dataMap.put("Tea_sex", input1);
        dataMap.put("Tea_major", input2);
        dataMap.put("Tea_posts", input3);
        dataMap.put("Tea_phone", input4);

        // 将Map对象添加到List中
        dataList.add(dataMap);

        System.out.println(dataList);

        ManagerService managerService = new ManagerServiceImpl();

        boolean tea = false;

        tea = managerService.addTeacher(dataList);
        System.out.println(tea);

        ResultInfo info = new ResultInfo();
        info.setFlag(tea);

        writeJsonValue(response, info);
    }

    public void mngSearchTeachers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngSearchTeachers");

        String searchText = request.getParameter("searchText");
        System.out.println(searchText);

        ManagerService managerService = new ManagerServiceImpl();
        List<Map<String, Object>> courses = managerService.findTeacher(searchText);

        for(Map map: courses)
            System.out.println(map);
        System.out.println(courses);

        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
        } catch (IOException e) {
            System.out.println("err");
        }
    }

    public void mngSearchStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngSearchStudents");

        String searchText = request.getParameter("searchText");
        System.out.println(searchText);

        ManagerService managerService = new ManagerServiceImpl();
        List<Map<String, Object>> courses = managerService.findStudent(searchText);

        for(Map map: courses)
            System.out.println(map);
        System.out.println(courses);

        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
        } catch (IOException e) {
            System.out.println("err");
        }
    }

    public void mngSearchPlans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("mngSearchPlans");

        String searchText = request.getParameter("searchText");
        System.out.println(searchText);

        ManagerService managerService = new ManagerServiceImpl();
        List<Map<String, Object>> courses = managerService.findPlan(searchText);

        for (Map<String, Object> plan : courses) {
            int couPass = (int) plan.get("Cou_pass");
            if (couPass == 1) {
                plan.put("Cou_pass", "已通过");
            } else if (couPass == 0) {
                plan.put("Cou_pass", "未通过");
            }
        }

        for(Map map: courses)
            System.out.println(map);
        System.out.println(courses);

        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
        } catch (IOException e) {
            System.out.println("err");
        }
    }

    public void SaveTeacher(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        //封装对象
        Map<String, String[]> parameterMap = request.getParameterMap();
        //空的申请表类
        Teacher teacher=new Teacher();
        //填表
        try {
            BeanUtils.populate(teacher, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ManagerService managerService=new ManagerServiceImpl();
        boolean flag=managerService.SaveTeacher(teacher);
        ResultInfo info=new ResultInfo();
        if (flag) {

            info.setFlag(true);
        } else {

            info.setFlag(false);
            info.setErrorMsg("保存讲师信息失败！");
        }
        // 将info序列化为json，并写回到客户端
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        // 将json写回客户端
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }

    public void SaveExecutor(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        //封装对象
        Map<String, String[]> parameterMap = request.getParameterMap();
        //空的申请表类
        Executor executor=new Executor();
        //填表
        try {
            BeanUtils.populate(executor, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        ManagerService managerService=new ManagerServiceImpl();
        boolean flag= managerService.SaveExecutor(executor);
        ResultInfo info=new ResultInfo();
        if (flag) {

            info.setFlag(true);
        } else {

            info.setFlag(false);
            info.setErrorMsg("保存执行人信息失败！");
        }
        // 将info序列化为json，并写回到客户端
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        // 将json写回客户端
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);
    }

}
